package cn.bdqn.witcity.service;

import cn.bdqn.witcity.entity.Role;
import cn.bdqn.witcity.entity.User;
import cn.bdqn.witcity.entity.vo.UserVo;
import cn.bdqn.witcity.mapper.UserMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserDetailsService,UserService{
    @Resource
    UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user=userMapper.loadUserByUsername(s);
        if(user==null){
            throw  new UsernameNotFoundException("用户名不存在");
        }
        user.setRoles(userMapper.getUserRolesByUid(user.getId()));
        return user;
    }

    @Override
    public List<Role> getUserRoleByUid(Integer id) {
        return userMapper.getUserRolesByUid(id);
    }

    @Override
    public List<User> getUserByPage(UserVo userVo) {
        return userMapper.getUserByPage(userVo);
    }

    @Override
    public int getUserCount(UserVo userVo) {
        return userMapper.getUserCount(userVo);
    }

    @Override
    public int addUser(User user, Integer type) {
        int result=userMapper.addUser(user);
        result+=userMapper.addUserRole(user.getId(),type);
        return result;
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public int delUser(Integer id) {
        int result=userMapper.delUser(id);
        result+=userMapper.delRoleUser(id);
        return result;
    }

    @Override
    public List<User> findBangke() {
        return userMapper.findBangKe();
    }

    @Override
    public int updateMoney(User user) {
        return userMapper.updateMoney(user);
    }

    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }
}
